<?php

/**
 * 店铺
 * @copyright (c) 2013, Mr.Wang
 * @author Mr.Wang <281762391@qq.com>
 */
class OrderAction extends HomeAction {

    public function _initialize() {
        parent::_initialize();
    }

    public function index() {
        $ids = array();
        foreach ($_POST['cai_num'] as $key => $value) {
            if ($value > 0) {
                $ids[] = $key;
            }
        }
        $where = array();
        $where['cai_id'] = array(
            'in',
            $ids
        );
        $cai = M("Cai")->where($where)->select();
        $this->assign("list", $cai);
        $this->assign("cai_num", $_POST['cai_num']);
        $this->display();
    }

    public function insert() {
        $cart_data = array(
            'cai_num' => session("cai_num"),
            'method'  => session("method"),
        );
        $cai_ids  = array_keys($cart_data['cai_num']);
        $where    = array(
            "cai_id" => array(
                "IN", $cai_ids
            )
        );

        $cai = M("Cai")->where($where)->select();

        $total_price = 0;
        $shop_id     = array();
        foreach ($cai as $value) {
            if (!in_array($value['cai_shop_id'], $shop_id)) {
                $shop_id[] = $value['cai_shop_id'];
            }
            $total_price+= $value['cai_price'] * $cart_data['cai_num'][$value['cai_id']];
        }

        if (count($shop_id) > 1) {
            $this->error("不能选择不同商家的商品进行下订单！");
        }
        $userExt = M("UserExt")->find(session("uid"));

        /**
         * 添加至ORDER
         */
        $addData = array(
            'uid'         => session("uid"),
            'price'       => $total_price,
            'shop_id'     => $shop_id[0],
            'pay_status'  => "0",
            'name'        => $userExt['name'],
            'mobile'      => $userExt['mobile'],
            'area'        => $userExt['user_area'],
            'address'     => $userExt['user_address'],
            'create_time' => time(),
            'pay_type'    => $cart_data['method'],
            'status'      => "0",
        );

        $orderID = M("Order")->add($addData);
        foreach ($cai as $value) {
            /**
             * 添加至OrderDetail
             */
            $addData = array(
                'order_id'  => $orderID,
                'cai_id'    => $value['cai_id'],
                'shop_id'   => $value['cai_shop_id'],
                'cai_name'  => $value['cai_name'],
                'cai_price' => $value['cai_price'],
                'cai_num'   => $cart_data['cai_num'][$value['cai_id']],
                'status'    => "0",
            );

            $orderID = M("OrderDetail")->add($addData);
        }
        $this->success("下订单成功", U("User/Index/Index"));
    }

    public function address() {
        session("cai_num", $_POST['cai_num']);
        session("method", $_POST['method']);
        $userExt = M("UserExt")->find(session('uid'));
        $this->assign("user_ext", $userExt);
        $this->assign("area", D('Area')->get($userExt['user_area']));
        $this->display();
    }

    public function cart() {
        
    }

}
